package com.example.helloworld

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment

class PomodoroFragment : Fragment() {
    private lateinit var timerText: TextView
    private lateinit var startButton: Button
    private lateinit var pomodoroCountText: TextView
    private var isRunning = false
    private var timeLeftInSeconds = 25 * 60 // 25分钟
    private val handler = Handler(Looper.getMainLooper())
    private var pomodoroCount = 0

    private val updateTimer = object : Runnable {
        override fun run() {
            if (isRunning && timeLeftInSeconds > 0) {
                timeLeftInSeconds--
                updateDisplay()
                handler.postDelayed(this, 1000)
            } else if (timeLeftInSeconds == 0) {
                // 时间到
                isRunning = false
                startButton.text = "开始专注"
                onPomodoroCompleted()
                // TODO: 添加提醒声音
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d("PomodoroFragment", "onCreate called")
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        Log.d("PomodoroFragment", "onCreateView called")
        return inflater.inflate(R.layout.fragment_pomodoro, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        timerText = view.findViewById(R.id.pomodoroTimerText)
        startButton = view.findViewById(R.id.startPomodoroButton)
        pomodoroCountText = view.findViewById(R.id.pomodoroCountText)

        loadPomodoroCount()
        updateDisplay()

        startButton.setOnClickListener {
            if (!isRunning) {
                isRunning = true
                startButton.text = "结束专注"
                handler.post(updateTimer)
            } else {
                isRunning = false
                timeLeftInSeconds = 25 * 60
                startButton.text = "开始专注"
                handler.removeCallbacks(updateTimer)
                updateDisplay()
            }
        }
    }

    private fun updateDisplay() {
        val minutes = timeLeftInSeconds / 60
        val seconds = timeLeftInSeconds % 60
        timerText.text = String.format("%02d:%02d", minutes, seconds)
    }

    private fun onPomodoroCompleted() {
        pomodoroCount++
        // 保存番茄数量
        requireContext().getSharedPreferences("pomodoro_prefs", Context.MODE_PRIVATE)
            .edit()
            .putInt("pomodoro_count", pomodoroCount)
            .apply()
        updatePomodoroCount()
    }

    private fun updatePomodoroCount() {
        pomodoroCountText.text = "🍅 × $pomodoroCount"
    }

    private fun loadPomodoroCount() {
        pomodoroCount = requireContext().getSharedPreferences("pomodoro_prefs", Context.MODE_PRIVATE)
            .getInt("pomodoro_count", 0)
        updatePomodoroCount()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        handler.removeCallbacks(updateTimer)
    }
} 